<?php

function assign_job($line,$ln)
{
	$month = array('Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12);
	$year = date('Y');
	preg_match('/(?P<date1>^[A-Z]{1}[a-z]{2}\s[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}) (?P<ip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) (?P<id>[0-9]{1,9}): (?P<date2>([A-Z]{1}[a-z]{2}\s[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}|.+)): %(?P<code1>.+)-(?P<code2>.+)-(?P<code3>[A-Z_]+):(?P<msg>.+)/', $line, $matches);
	
	if(preg_match("/local Listening for Syslog messages/",$line))
	{
		return $ln." -- local Listening for Syslog messages";
	}
	else
	{
		$code = $matches['code1'].'-'.$matches['code2'].'-'.$matches['code3'];
	}
	
	if($code == 'LINK-3-UPDOWN')
	{
		if(preg_match("/changed state to up/",$matches['msg']))
		{
			$ip = new Ip;
			$state = new Error;
			$state->where('ip_id = '.$ip->get_by_wan($matches['ip'])->id.' and facility_id = 274 and severity_id = 4 and mnemonic_id = 1 and status = \'Pending\'')->order_by('created desc')->get();
			if($state->exists())
			{
				$state->close_date = date("Y-m-d H:i:s");
				$state->status = "Closed";
				$state->user_message = "Closed by System";
				$state->save();
				return $ln." -- Datetime: ".$state->errordate.'<br />IP Address: '.$matches['ip'].'<br />Error: '.$code.'<br />Status: Job Closed';
			}
			else
			{
				return $ln." -- ".$line;
			}
		}
		else
		{
			$ip = new Ip;
		exec('ping '.$ip->get_by_wan($matches['ip'])->loopback, $data);
			if(preg_match("/Reply from/",$data[2]))
			{
				$assignLevel = '2';
			}
			else
			{
				$assignLevel = '3';
			}
			/*$assign = new Assign;
			$assign->where("error_message like '%$code%'")->get();
			if($assign->exists())
			{
				$email['error'] = $code;
				$email['explanation'] = $assign->explanation;
				$email['recommended'] = $assign->recommended;				
			}	*/
		}
	}
	else
	{
		$assign_match = new Assign_match;
		$assign_match->where("error_message like '%$code%'")->get();
		if($assign_match->exists())
		{
			$state = new Error($assign_match->assign_id);
			$state->status = "Closed";
			$state->user_message = "Closed by System";
			$state->save();
			return $ln." -- Datetime: ".$state->errordate.'<br />IP Address: '.$matches['ip'].'<br />Error: '.$code.'<br />Status: Job Closed';
		}
		else
		{
			$ip = new Ip;
			$facility = new Facility;
			$severity = new Severity;
			$mnemonic = new Mnemonic;
			$mnemonic->get_by_name($matches['code3']);
			$checkError = new Error;
			$checkError->where('ip_id',$ip->get_by_wan($matches['ip'])->id);
			$checkError->where('facility_id',$facility->get_by_code($matches['code1'])->id);
			$checkError->where('severity_id',$severity->get_by_code($matches['code2'])->id);
			$checkError->where('mnemonic_id',$mnemonic->id);
			$checkError->get();
			if(!$checkError->exists())
			{
				$assign = new Assign;
				$assign->where("error_message like '%$code%'")->get();
				if($assign->exists())
				{
					/*$email['error'] = $code;
					$email['explanation'] = $assign->explanation;
					$email['recommended'] = $assign->recommended;*/		
					$assignLevel = $assign->level_id;
				}
				else
				{
					return $ln." -- ".$line;
				}
			}
			else
			{
				return $ln." -- ".$line;
			}
		}
	}
	
	if(isset($assignLevel))
	{
		$checkuser = new Error;
		$checkuser->sql("select users.*,ifnull(a.num,0) num
						from users left join (select user_id,count(user_id) num from errores where user_id in (select id from users where level_id = ".$assignLevel.") and status = 'Pending' group by user_id) a
						on a.user_id = users.id
						where users.level_id = ".$assignLevel."
						order by num
						")->get_page(1);
		/*$email['email'] = $checkuser->email;*/
		$ip = new Ip;
		$facility = new Facility;
		list($mon,$date,$time) = explode(' ', $matches['date1']);
		$severity = new Severity;
		$mnemonic = new Mnemonic;
		$mnemonic->get_by_name($matches['code3']);
		$mnemonic->name = $matches['code3'];
		$mnemonic->save();
		
		$array['user_id'] = $checkuser->id;
		$array['ip_id'] = $ip->get_by_wan($matches['ip'])->id;
		$array['facility_id'] = $facility->get_by_code($matches['code1'])->id;
		$array['error_date'] = $year.'-'.$month[$mon].'-'.$date.' '.$time;
		$array['severity_id'] = $severity->get_by_code($matches['code2'])->id;
		$array['mnemonic_id'] = $mnemonic->id;
		$array['msg'] = $matches['msg'];
						
		$error = new Error;
		$error->from_array($array);
		$error->save();
		/*$email['branch'] = $ip->branch->name;
		if($assignLevel<>2)assign_mail($email);*/
		$log = $ln." -- Datetime: ".$array['error_date'].'<br />IP Address: '.$matches['ip'].'<br />Error: '.$code.'<br />Assing To: '.$checkuser->fullname;
		return $log;
	}
	else
	{
		return $ln." -- ".$line;
	}
	
}

function assign_mail($data)
{
	$html='<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">&nbsp;</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">เรียน </span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">'.$branch.'</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">เรื่อง<span style="">&nbsp;
</span>แจ้งตรวจสอบอุปกรณ์เครือข่าย</span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;"></span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">&nbsp;</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ตามที่บริษัทฯ
ได้ตรวจพบข้อความแจ้งเตือน </span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">'.$error.' <span lang="TH">ซึ่งปัญหาอาจเกิดจาก </span>'.$explanation.' <span lang="TH">จึงขอให้ท่านทำการตรวจสอบตามคำแนะนำดังนี้</span></span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="">&nbsp;</span></span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">'.$recommended.'</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">&nbsp;</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span lang="TH">บริษัทฯ
ขอขอบคุณที่ท่านให้ความร่วมมือในการตรวจสอบ มา ณ ที่นี้</span></span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">&nbsp;</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">&nbsp;</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">ขอแสดงความนับถือ</span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;"></span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">บริษัท เอบีซี เท็คโนโลยี จำกัด</span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;"></span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">เลขที่ </span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">65/17 <span lang="TH">แจ้งวัฒนะซอย </span>1</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">ถนนแจ้งวัฒนะ<span style="">&nbsp; </span>แขวงตลาดบางเขต</span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;"></span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">เขตหลักสี่
กรุงเทพฯ </span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;">10210</span></p>

<p class="MsoNormal"><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;" lang="TH">โทร</span><span style="font-size: 14pt; font-family: &quot;Cordia New&quot;,&quot;sans-serif&quot;;"> 02-956-2339, 02-968-7517</span></p>';

$CI =& get_instance();
$CI->email->from('marisa_ck@hotmail.com', 'บริษัท เอบีซี เท็คโนโลยี จำกัด');
$CI->email->to($email);
$CI->email->subject('แจ้งตรวจสอบอุปกรณ์เครือข่าย');
$CI->email->message($html);
$CI->email->send();
}
?>